package com.softeem.hbsm_video.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.hbsm_video.entity.Comment;
import com.softeem.hbsm_video.entity.User;
import com.softeem.hbsm_video.entity.Video;
import com.softeem.hbsm_video.service.CommentService;
import com.softeem.hbsm_video.service.UserService;
import com.softeem.hbsm_video.service.VideoService;
import com.softeem.hbsm_video.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhouYu
 * @version 1.0
 * 评论业务接口
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        List<Comment> list = commentService.list(new Page<Comment>(page, limit));

        list.forEach(v->{
            // 获取作者id
            Integer uid = v.getUid();
            // 获取视频id
            Integer vid = v.getVid();
            //根据作者id查询作者
            User user = userService.getById(uid);
            Video video = videoService.getById(vid);
            //将视频的作者和频道设置到视频对象中
            v.setUser(user);
            v.setVideo(video);
        });

        //查询总记录数
        long count = commentService.count();
        return R.ok("查询成功").setData(list).setCount(count);
    }

    @PostMapping("/update")
    public R update(@RequestBody Comment comment){
        boolean b = commentService.updateById(comment);
        if (b){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }

    @PostMapping("/delete")
    public R delete(Integer id){
        boolean b = commentService.removeById(id);
        if (b){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
}
